package com.jy.app.lib.utils;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.widget.EditText;

import com.jy.app.lib.base.ParametersConfig;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Date;

/**
 * Created by Administrator on 2017/10/18.
 */

public class ChangeNetUtil {
    static long time1 = 0;
    static int times = 0;

    public static void changeNet(final View view1, View view2) {
        Class<?> clazz = null;
        try {
            clazz = Class.forName(view1.getContext().getPackageName() + ".contants.URL");
            Field modeField = clazz.getDeclaredField("CURRENT_MODE");
            modeField.setAccessible(true);//只有这里设置为true才可以修改
            Object obj = modeField.get(clazz);
            if("3".equals(obj.toString())){
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        view1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                long time = new Date().getTime();
                long cc = time - time1;
                if (time1 == 0 || cc<= 500) {
                    time1 = time;
                    times++;
                } else {
                    times = 0;
                    time1 = 0;
                }

            }
        });
        view2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                long time = new Date().getTime();
                long cc = time - time1;
                if (times >= 3 || cc <= 500) {
                    try {
                        showDialog(view1.getContext());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    times = 0;
                    time1 = 0;
                } else {
                    times = 0;
                    time1 = 0;
                }
            }
        });
    }

    public static void showDialog(final Context context) throws Exception {
        Class<?> clazz  = Class.forName(context.getPackageName() + ".contants.URL");
        Method method = clazz.getDeclaredMethod("getBaseUrl");
        method.setAccessible(true);
        Object object = method.invoke(clazz);
        final EditText text = new EditText(context);
        text.setText(object.toString());
        new AlertDialog.Builder(context).setTitle("请输入服务器地址：").setView(text).setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                String newip = text.getText().toString();
                try {
                    ParametersConfig.getInstance().setString("DEV_URL",newip);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).setNegativeButton("取消", null).show();
    }
}
